//
//  PersonInfoViewController.m
//  HDYS
//
//  Created by qianfeng on 15-5-6.
//  Copyright (c) 2015年 kay. All rights reserved.
//

#import "PersonInfoViewController.h"
#import "PersonIconTableViewCell.h"
#import "PersonInfoTableViewCell.h"

#import "PickIconViewController.h"
#import "ChangeInfoVC.h"
#import "ChangeSexViewController.h"

#define SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width
#define SCREEN_HEIGHT [UIScreen mainScreen].bounds.size.height


@interface PersonInfoViewController () <UITableViewDataSource, UITableViewDelegate, UIActionSheetDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate, ChangeInfoVCDelegate>
{
    NSMutableArray *_dataArray;
    
    UITableView *_tableView;
}
@end

@implementation PersonInfoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBar.translucent = NO;
    
    [self createData];
    [self createTableView];
}

- (void)createData {
    _dataArray = [NSMutableArray arrayWithCapacity:0];
    
    NSArray *array1 = @[@"名字", @"性别", @"地址", @"生日"];
    NSArray *array2 = @[@"kay", @"男", @"上海市杨浦区", @"1999-6-8"];
    for (int i = 0; i < array1.count; i++) {
        NSMutableDictionary *dic = [NSMutableDictionary dictionary];
        [dic setObject:array2[i] forKey:array1[i]];
        [_dataArray addObject:dic];
    }
    
}

- (void)createTableView {
    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 64) style:UITableViewStyleGrouped];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return 1;
    } else if (section == 1) {
        return _dataArray.count;
    }
    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        static NSString *cellIdentifier = @"PersonIconCellId";
        PersonIconTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
        if (cell == nil) {
            cell = [[[NSBundle mainBundle] loadNibNamed:@"PersonIconTableViewCell" owner:self options:nil] lastObject];
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        }
        return cell;
    } else {
        static NSString *cellIdentifier = @"PersonInfoCellId";
        PersonInfoTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
        if (cell == nil) {
            cell = [[[NSBundle mainBundle] loadNibNamed:@"PersonInfoTableViewCell" owner:self options:nil] lastObject];
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        }
        [cell config:_dataArray[indexPath.row]];
        return cell;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        return 100.0f;
    }
    return 44.0f;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        //修改头像
        UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"选择头像" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"相册选择",@"手机拍照", nil];
        [actionSheet showFromTabBar:self.tabBarController.tabBar];
        actionSheet.actionSheetStyle = UIActionSheetStyleBlackTranslucent;
    } else if (indexPath.section == 1 && indexPath.row == 0) {
        //修改用户名
        ChangeInfoVC *changeVC = [[ChangeInfoVC alloc] initWithNibName:@"ChangeInfoVC" bundle:nil];
        changeVC.delegate = self;
        changeVC.infoDic = _dataArray[indexPath.row];
        [self presentViewController:changeVC animated:YES completion:nil];
    } else if (indexPath.section == 1 && indexPath.row == 1) {
        //修改性别
        ChangeSexViewController *changeSexVC = [[ChangeSexViewController alloc] init];
        [self.navigationController pushViewController:changeSexVC animated:YES];
    } else if (indexPath.section == 1 && indexPath.row == 2) {
        //修改所在地
        
    } else if (indexPath.section == 1 && indexPath.row == 3) {
        //修改生日
    }
}

#pragma mark - ChangeInfoVCDelegate代理 
- (void)updatePersonInfoWithDic:(NSDictionary *)infoDic {
    for (NSMutableDictionary *dic in _dataArray) {
        if ([[dic allKeys].lastObject isEqualToString:[infoDic allKeys].lastObject]) {
            [dic setObject:[infoDic allValues].lastObject forKey:[dic allKeys].lastObject];
            [_tableView reloadData];
            return;
        }
    }
}

#pragma mark - UIActionSheetDelegate代理
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (buttonIndex == 0) {
        UIImagePickerController *imgPickerCtrl = [[UIImagePickerController alloc] init];
        imgPickerCtrl.delegate = self;
        imgPickerCtrl.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        [self presentViewController:imgPickerCtrl animated:YES completion:nil];
        
    } else if (buttonIndex == 1) {
        
    }
}

#pragma mark - UIImagePickerControllerDelegate代理
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
    [picker dismissViewControllerAnimated:YES completion:nil];
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo NS_DEPRECATED_IOS(2_0, 3_0) {
    NSLog(@"2222222222");
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    //选取照片后
    PickIconViewController *pickIconVC = [[PickIconViewController alloc] initWithNibName:@"PickIconViewController" bundle:nil];
    pickIconVC.image = info[UIImagePickerControllerOriginalImage];
    [picker presentViewController:pickIconVC animated:YES completion:nil];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
